

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>20.4. wsgiref — WSGI Utilities and Reference Implementation &mdash; Python v2.7.8 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.8',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7.8 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7.8 documentation" href="../index.html" />
    <link rel="up" title="20. Internet Protocols and Support" href="internet.html" />
    <link rel="next" title="20.5. urllib — Open arbitrary resources by URL" href="urllib.html" />
    <link rel="prev" title="20.3. cgitb — Traceback manager for CGI scripts" href="cgitb.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="urllib.html" title="20.5. urllib — Open arbitrary resources by URL"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="cgitb.html" title="20.3. cgitb — Traceback manager for CGI scripts"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="internet.html" accesskey="U">20. Internet Protocols and Support</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-wsgiref">
<span id="wsgiref-wsgi-utilities-and-reference-implementation"></span><h1>20.4. <a class="reference internal" href="#module-wsgiref" title="wsgiref: WSGI Utilities and Reference Implementation."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref</span></tt></a> &#8212; WSGI Utilities and Reference Implementation<a class="headerlink" href="#module-wsgiref" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 2.5.</span></p>
<p>The Web Server Gateway Interface (WSGI) is a standard interface between web
server software and web applications written in Python. Having a standard
interface makes it easy to use an application that supports WSGI with a number
of different web servers.</p>
<p>Only authors of web servers and programming frameworks need to know every detail
and corner case of the WSGI design.  You don&#8217;t need to understand every detail
of WSGI just to install a WSGI application or to write a web application using
an existing framework.</p>
<p><a class="reference internal" href="#module-wsgiref" title="wsgiref: WSGI Utilities and Reference Implementation."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref</span></tt></a> is a reference implementation of the WSGI specification that can
be used to add WSGI support to a web server or framework.  It provides utilities
for manipulating WSGI environment variables and response headers, base classes
for implementing WSGI servers, a demo HTTP server that serves WSGI applications,
and a validation tool that checks WSGI servers and applications for conformance
to the WSGI specification (<span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>).</p>
<p>See <a class="reference external" href="http://www.wsgi.org">http://www.wsgi.org</a> for more information about WSGI, and links to tutorials
and other resources.</p>
<div class="section" id="module-wsgiref.util">
<span id="wsgiref-util-wsgi-environment-utilities"></span><h2>20.4.1. <a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.util</span></tt></a> &#8211; WSGI environment utilities<a class="headerlink" href="#module-wsgiref.util" title="Permalink to this headline">¶</a></h2>
<p>This module provides a variety of utility functions for working with WSGI
environments.  A WSGI environment is a dictionary containing HTTP request
variables as described in <span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.  All of the functions taking an <em>environ</em>
parameter expect a WSGI-compliant dictionary to be supplied; please see
<span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a> for a detailed specification.</p>
<dl class="function">
<dt id="wsgiref.util.guess_scheme">
<tt class="descclassname">wsgiref.util.</tt><tt class="descname">guess_scheme</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#wsgiref.util.guess_scheme" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a guess for whether <tt class="docutils literal"><span class="pre">wsgi.url_scheme</span></tt> should be &#8220;http&#8221; or &#8220;https&#8221;, by
checking for a <tt class="docutils literal"><span class="pre">HTTPS</span></tt> environment variable in the <em>environ</em> dictionary.  The
return value is a string.</p>
<p>This function is useful when creating a gateway that wraps CGI or a CGI-like
protocol such as FastCGI.  Typically, servers providing such protocols will
include a <tt class="docutils literal"><span class="pre">HTTPS</span></tt> variable with a value of &#8220;1&#8221; &#8220;yes&#8221;, or &#8220;on&#8221; when a request
is received via SSL.  So, this function returns &#8220;https&#8221; if such a value is
found, and &#8220;http&#8221; otherwise.</p>
</dd></dl>

<dl class="function">
<dt id="wsgiref.util.request_uri">
<tt class="descclassname">wsgiref.util.</tt><tt class="descname">request_uri</tt><big>(</big><em>environ</em>, <em>include_query=1</em><big>)</big><a class="headerlink" href="#wsgiref.util.request_uri" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the full request URI, optionally including the query string, using the
algorithm found in the &#8220;URL Reconstruction&#8221; section of <span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.  If
<em>include_query</em> is false, the query string is not included in the resulting URI.</p>
</dd></dl>

<dl class="function">
<dt id="wsgiref.util.application_uri">
<tt class="descclassname">wsgiref.util.</tt><tt class="descname">application_uri</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#wsgiref.util.application_uri" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <a class="reference internal" href="#wsgiref.util.request_uri" title="wsgiref.util.request_uri"><tt class="xref py py-func docutils literal"><span class="pre">request_uri()</span></tt></a>, except that the <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> and
<tt class="docutils literal"><span class="pre">QUERY_STRING</span></tt> variables are ignored.  The result is the base URI of the
application object addressed by the request.</p>
</dd></dl>

<dl class="function">
<dt id="wsgiref.util.shift_path_info">
<tt class="descclassname">wsgiref.util.</tt><tt class="descname">shift_path_info</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#wsgiref.util.shift_path_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Shift a single name from <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> to <tt class="docutils literal"><span class="pre">SCRIPT_NAME</span></tt> and return the name.
The <em>environ</em> dictionary is <em>modified</em> in-place; use a copy if you need to keep
the original <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> or <tt class="docutils literal"><span class="pre">SCRIPT_NAME</span></tt> intact.</p>
<p>If there are no remaining path segments in <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt>, <tt class="xref docutils literal"><span class="pre">None</span></tt> is returned.</p>
<p>Typically, this routine is used to process each portion of a request URI path,
for example to treat the path as a series of dictionary keys. This routine
modifies the passed-in environment to make it suitable for invoking another WSGI
application that is located at the target URI. For example, if there is a WSGI
application at <tt class="docutils literal"><span class="pre">/foo</span></tt>, and the request URI path is <tt class="docutils literal"><span class="pre">/foo/bar/baz</span></tt>, and the
WSGI application at <tt class="docutils literal"><span class="pre">/foo</span></tt> calls <a class="reference internal" href="#wsgiref.util.shift_path_info" title="wsgiref.util.shift_path_info"><tt class="xref py py-func docutils literal"><span class="pre">shift_path_info()</span></tt></a>, it will receive the
string &#8220;bar&#8221;, and the environment will be updated to be suitable for passing to
a WSGI application at <tt class="docutils literal"><span class="pre">/foo/bar</span></tt>.  That is, <tt class="docutils literal"><span class="pre">SCRIPT_NAME</span></tt> will change from
<tt class="docutils literal"><span class="pre">/foo</span></tt> to <tt class="docutils literal"><span class="pre">/foo/bar</span></tt>, and <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> will change from <tt class="docutils literal"><span class="pre">/bar/baz</span></tt> to
<tt class="docutils literal"><span class="pre">/baz</span></tt>.</p>
<p>When <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> is just a &#8220;/&#8221;, this routine returns an empty string and
appends a trailing slash to <tt class="docutils literal"><span class="pre">SCRIPT_NAME</span></tt>, even though empty path segments are
normally ignored, and <tt class="docutils literal"><span class="pre">SCRIPT_NAME</span></tt> doesn&#8217;t normally end in a slash.  This is
intentional behavior, to ensure that an application can tell the difference
between URIs ending in <tt class="docutils literal"><span class="pre">/x</span></tt> from ones ending in <tt class="docutils literal"><span class="pre">/x/</span></tt> when using this
routine to do object traversal.</p>
</dd></dl>

<dl class="function">
<dt id="wsgiref.util.setup_testing_defaults">
<tt class="descclassname">wsgiref.util.</tt><tt class="descname">setup_testing_defaults</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#wsgiref.util.setup_testing_defaults" title="Permalink to this definition">¶</a></dt>
<dd><p>Update <em>environ</em> with trivial defaults for testing purposes.</p>
<p>This routine adds various parameters required for WSGI, including <tt class="docutils literal"><span class="pre">HTTP_HOST</span></tt>,
<tt class="docutils literal"><span class="pre">SERVER_NAME</span></tt>, <tt class="docutils literal"><span class="pre">SERVER_PORT</span></tt>, <tt class="docutils literal"><span class="pre">REQUEST_METHOD</span></tt>, <tt class="docutils literal"><span class="pre">SCRIPT_NAME</span></tt>,
<tt class="docutils literal"><span class="pre">PATH_INFO</span></tt>, and all of the <span class="target" id="index-4"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>-defined <tt class="docutils literal"><span class="pre">wsgi.*</span></tt> variables.  It
only supplies default values, and does not replace any existing settings for
these variables.</p>
<p>This routine is intended to make it easier for unit tests of WSGI servers and
applications to set up dummy environments.  It should NOT be used by actual WSGI
servers or applications, since the data is fake!</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">wsgiref.util</span> <span class="kn">import</span> <span class="n">setup_testing_defaults</span>
<span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>

<span class="c"># A relatively simple WSGI application. It&#39;s going to print out the</span>
<span class="c"># environment dictionary after being updated by setup_testing_defaults</span>
<span class="k">def</span> <span class="nf">simple_app</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">setup_testing_defaults</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>

    <span class="n">status</span> <span class="o">=</span> <span class="s">&#39;200 OK&#39;</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s">&#39;text/plain&#39;</span><span class="p">)]</span>

    <span class="n">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>

    <span class="n">ret</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
           <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">environ</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()]</span>
    <span class="k">return</span> <span class="n">ret</span>

<span class="n">httpd</span> <span class="o">=</span> <span class="n">make_server</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">simple_app</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Serving on port 8000...&quot;</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<p>In addition to the environment functions above, the <a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.util</span></tt></a> module
also provides these miscellaneous utilities:</p>
<dl class="function">
<dt id="wsgiref.util.is_hop_by_hop">
<tt class="descclassname">wsgiref.util.</tt><tt class="descname">is_hop_by_hop</tt><big>(</big><em>header_name</em><big>)</big><a class="headerlink" href="#wsgiref.util.is_hop_by_hop" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if &#8216;header_name&#8217; is an HTTP/1.1 &#8220;Hop-by-Hop&#8221; header, as defined by
<span class="target" id="index-5"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a>.</p>
</dd></dl>

<dl class="class">
<dt id="wsgiref.util.FileWrapper">
<em class="property">class </em><tt class="descclassname">wsgiref.util.</tt><tt class="descname">FileWrapper</tt><big>(</big><em>filelike</em>, <em>blksize=8192</em><big>)</big><a class="headerlink" href="#wsgiref.util.FileWrapper" title="Permalink to this definition">¶</a></dt>
<dd><p>A wrapper to convert a file-like object to an <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a>.  The resulting objects
support both <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> and <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> iteration styles, for
compatibility with Python 2.1 and Jython. As the object is iterated over, the
optional <em>blksize</em> parameter will be repeatedly passed to the <em>filelike</em>
object&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> method to obtain strings to yield.  When <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt>
returns an empty string, iteration is ended and is not resumable.</p>
<p>If <em>filelike</em> has a <tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt> method, the returned object will also have a
<tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt> method, and it will invoke the <em>filelike</em> object&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt>
method when called.</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span>
<span class="kn">from</span> <span class="nn">wsgiref.util</span> <span class="kn">import</span> <span class="n">FileWrapper</span>

<span class="c"># We&#39;re using a StringIO-buffer for as the file-like object</span>
<span class="n">filelike</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">&quot;This is an example file-like object&quot;</span><span class="o">*</span><span class="mi">10</span><span class="p">)</span>
<span class="n">wrapper</span> <span class="o">=</span> <span class="n">FileWrapper</span><span class="p">(</span><span class="n">filelike</span><span class="p">,</span> <span class="n">blksize</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>

<span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">wrapper</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">chunk</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="module-wsgiref.headers">
<span id="wsgiref-headers-wsgi-response-header-tools"></span><h2>20.4.2. <a class="reference internal" href="#module-wsgiref.headers" title="wsgiref.headers: WSGI response header tools."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.headers</span></tt></a> &#8211; WSGI response header tools<a class="headerlink" href="#module-wsgiref.headers" title="Permalink to this headline">¶</a></h2>
<p>This module provides a single class, <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a>, for convenient
manipulation of WSGI response headers using a mapping-like interface.</p>
<dl class="class">
<dt id="wsgiref.headers.Headers">
<em class="property">class </em><tt class="descclassname">wsgiref.headers.</tt><tt class="descname">Headers</tt><big>(</big><em>headers</em><big>)</big><a class="headerlink" href="#wsgiref.headers.Headers" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a mapping-like object wrapping <em>headers</em>, which must be a list of header
name/value tuples as described in <span class="target" id="index-6"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.  Any changes made to the new
<a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a> object will directly update the <em>headers</em> list it was created
with.</p>
<p><a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a> objects support typical mapping operations including
<a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a>, <tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></tt></a>, <tt class="xref py py-meth docutils literal"><span class="pre">setdefault()</span></tt>,
<a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a> and <tt class="xref py py-meth docutils literal"><span class="pre">has_key()</span></tt>.  For each of
these methods, the key is the header name (treated case-insensitively), and the
value is the first value associated with that header name.  Setting a header
deletes any existing values for that header, then adds a new value at the end of
the wrapped header list.  Headers&#8217; existing order is generally maintained, with
new headers added to the end of the wrapped list.</p>
<p>Unlike a dictionary, <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a> objects do not raise an error when you try
to get or delete a key that isn&#8217;t in the wrapped header list. Getting a
nonexistent header just returns <tt class="xref docutils literal"><span class="pre">None</span></tt>, and deleting a nonexistent header does
nothing.</p>
<p><a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a> objects also support <tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt>, and
<tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt> methods.  The lists returned by <tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt> can
include the same key more than once if there is a multi-valued header.  The
<tt class="docutils literal"><span class="pre">len()</span></tt> of a <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a> object is the same as the length of its
<tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt>, which is the same as the length of the wrapped header list.  In
fact, the <tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt> method just returns a copy of the wrapped header list.</p>
<p>Calling <tt class="docutils literal"><span class="pre">str()</span></tt> on a <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a> object returns a formatted string
suitable for transmission as HTTP response headers.  Each header is placed on a
line with its value, separated by a colon and a space. Each line is terminated
by a carriage return and line feed, and the string is terminated with a blank
line.</p>
<p>In addition to their mapping interface and formatting features, <a class="reference internal" href="#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt></a>
objects also have the following methods for querying and adding multi-valued
headers, and for adding headers with MIME parameters:</p>
<dl class="method">
<dt id="wsgiref.headers.Headers.get_all">
<tt class="descname">get_all</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#wsgiref.headers.Headers.get_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of all the values for the named header.</p>
<p>The returned list will be sorted in the order they appeared in the original
header list or were added to this instance, and may contain duplicates.  Any
fields deleted and re-inserted are always appended to the header list.  If no
fields exist with the given name, returns an empty list.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.headers.Headers.add_header">
<tt class="descname">add_header</tt><big>(</big><em>name</em>, <em>value</em>, <em>**_params</em><big>)</big><a class="headerlink" href="#wsgiref.headers.Headers.add_header" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a (possibly multi-valued) header, with optional MIME parameters specified
via keyword arguments.</p>
<p><em>name</em> is the header field to add.  Keyword arguments can be used to set MIME
parameters for the header field.  Each parameter must be a string or <tt class="xref docutils literal"><span class="pre">None</span></tt>.
Underscores in parameter names are converted to dashes, since dashes are illegal
in Python identifiers, but many MIME parameter names include dashes.  If the
parameter value is a string, it is added to the header value parameters in the
form <tt class="docutils literal"><span class="pre">name=&quot;value&quot;</span></tt>. If it is <tt class="xref docutils literal"><span class="pre">None</span></tt>, only the parameter name is added.
(This is used for MIME parameters without a value.)  Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">h</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s">&#39;content-disposition&#39;</span><span class="p">,</span> <span class="s">&#39;attachment&#39;</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s">&#39;bud.gif&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The above will add a header that looks like this:</p>
<div class="highlight-python"><pre>Content-Disposition: attachment; filename="bud.gif"</pre>
</div>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-wsgiref.simple_server">
<span id="wsgiref-simple-server-a-simple-wsgi-http-server"></span><h2>20.4.3. <a class="reference internal" href="#module-wsgiref.simple_server" title="wsgiref.simple_server: A simple WSGI HTTP server."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.simple_server</span></tt></a> &#8211; a simple WSGI HTTP server<a class="headerlink" href="#module-wsgiref.simple_server" title="Permalink to this headline">¶</a></h2>
<p>This module implements a simple HTTP server (based on <a class="reference internal" href="basehttpserver.html#module-BaseHTTPServer" title="BaseHTTPServer: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer)."><tt class="xref py py-mod docutils literal"><span class="pre">BaseHTTPServer</span></tt></a>)
that serves WSGI applications.  Each server instance serves a single WSGI
application on a given host and port.  If you want to serve multiple
applications on a single host and port, you should create a WSGI application
that parses <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> to select which application to invoke for each
request.  (E.g., using the <tt class="xref py py-func docutils literal"><span class="pre">shift_path_info()</span></tt> function from
<a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.util</span></tt></a>.)</p>
<dl class="function">
<dt id="wsgiref.simple_server.make_server">
<tt class="descclassname">wsgiref.simple_server.</tt><tt class="descname">make_server</tt><big>(</big><em>host</em>, <em>port</em>, <em>app</em>, <em>server_class=WSGIServer</em>, <em>handler_class=WSGIRequestHandler</em><big>)</big><a class="headerlink" href="#wsgiref.simple_server.make_server" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new WSGI server listening on <em>host</em> and <em>port</em>, accepting connections
for <em>app</em>.  The return value is an instance of the supplied <em>server_class</em>, and
will process requests using the specified <em>handler_class</em>.  <em>app</em> must be a WSGI
application object, as defined by <span class="target" id="index-7"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span><span class="p">,</span> <span class="n">demo_app</span>

<span class="n">httpd</span> <span class="o">=</span> <span class="n">make_server</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">demo_app</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Serving HTTP on port 8000...&quot;</span>

<span class="c"># Respond to requests until process is killed</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>

<span class="c"># Alternative: serve one request, then exit</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">handle_request</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="wsgiref.simple_server.demo_app">
<tt class="descclassname">wsgiref.simple_server.</tt><tt class="descname">demo_app</tt><big>(</big><em>environ</em>, <em>start_response</em><big>)</big><a class="headerlink" href="#wsgiref.simple_server.demo_app" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is a small but complete WSGI application that returns a text page
containing the message &#8220;Hello world!&#8221; and a list of the key/value pairs provided
in the <em>environ</em> parameter.  It&#8217;s useful for verifying that a WSGI server (such
as <a class="reference internal" href="#module-wsgiref.simple_server" title="wsgiref.simple_server: A simple WSGI HTTP server."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.simple_server</span></tt></a>) is able to run a simple WSGI application
correctly.</p>
</dd></dl>

<dl class="class">
<dt id="wsgiref.simple_server.WSGIServer">
<em class="property">class </em><tt class="descclassname">wsgiref.simple_server.</tt><tt class="descname">WSGIServer</tt><big>(</big><em>server_address</em>, <em>RequestHandlerClass</em><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIServer" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt></a> instance.  <em>server_address</em> should be a
<tt class="docutils literal"><span class="pre">(host,port)</span></tt> tuple, and <em>RequestHandlerClass</em> should be the subclass of
<a class="reference internal" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler" title="BaseHTTPServer.BaseHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></tt></a> that will be used to process
requests.</p>
<p>You do not normally need to call this constructor, as the <a class="reference internal" href="#wsgiref.simple_server.make_server" title="wsgiref.simple_server.make_server"><tt class="xref py py-func docutils literal"><span class="pre">make_server()</span></tt></a>
function can handle all the details for you.</p>
<p><a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt></a> is a subclass of <a class="reference internal" href="basehttpserver.html#BaseHTTPServer.HTTPServer" title="BaseHTTPServer.HTTPServer"><tt class="xref py py-class docutils literal"><span class="pre">BaseHTTPServer.HTTPServer</span></tt></a>, so all
of its methods (such as <tt class="xref py py-meth docutils literal"><span class="pre">serve_forever()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">handle_request()</span></tt>) are
available. <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt></a> also provides these WSGI-specific methods:</p>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIServer.set_app">
<tt class="descname">set_app</tt><big>(</big><em>application</em><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIServer.set_app" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the callable <em>application</em> as the WSGI application that will receive
requests.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.simple_server.WSGIServer.get_app">
<tt class="descname">get_app</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIServer.get_app" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the currently-set application callable.</p>
</dd></dl>

<p>Normally, however, you do not need to use these additional methods, as
<a class="reference internal" href="#wsgiref.simple_server.WSGIServer.set_app" title="wsgiref.simple_server.WSGIServer.set_app"><tt class="xref py py-meth docutils literal"><span class="pre">set_app()</span></tt></a> is normally called by <a class="reference internal" href="#wsgiref.simple_server.make_server" title="wsgiref.simple_server.make_server"><tt class="xref py py-func docutils literal"><span class="pre">make_server()</span></tt></a>, and the
<a class="reference internal" href="#wsgiref.simple_server.WSGIServer.get_app" title="wsgiref.simple_server.WSGIServer.get_app"><tt class="xref py py-meth docutils literal"><span class="pre">get_app()</span></tt></a> exists mainly for the benefit of request handler instances.</p>
</dd></dl>

<dl class="class">
<dt id="wsgiref.simple_server.WSGIRequestHandler">
<em class="property">class </em><tt class="descclassname">wsgiref.simple_server.</tt><tt class="descname">WSGIRequestHandler</tt><big>(</big><em>request</em>, <em>client_address</em>, <em>server</em><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Create an HTTP handler for the given <em>request</em> (i.e. a socket), <em>client_address</em>
(a <tt class="docutils literal"><span class="pre">(host,port)</span></tt> tuple), and <em>server</em> (<a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt></a> instance).</p>
<p>You do not need to create instances of this class directly; they are
automatically created as needed by <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt></a> objects.  You can,
however, subclass this class and supply it as a <em>handler_class</em> to the
<a class="reference internal" href="#wsgiref.simple_server.make_server" title="wsgiref.simple_server.make_server"><tt class="xref py py-func docutils literal"><span class="pre">make_server()</span></tt></a> function.  Some possibly relevant methods for overriding in
subclasses:</p>
<dl class="method">
<dt id="wsgiref.simple_server.WSGIRequestHandler.get_environ">
<tt class="descname">get_environ</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler.get_environ" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a dictionary containing the WSGI environment for a request.  The default
implementation copies the contents of the <a class="reference internal" href="#wsgiref.simple_server.WSGIServer" title="wsgiref.simple_server.WSGIServer"><tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt></a> object&#8217;s
<tt class="xref py py-attr docutils literal"><span class="pre">base_environ</span></tt> dictionary attribute and then adds various headers derived
from the HTTP request.  Each call to this method should return a new dictionary
containing all of the relevant CGI environment variables as specified in
<span class="target" id="index-8"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.simple_server.WSGIRequestHandler.get_stderr">
<tt class="descname">get_stderr</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler.get_stderr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the object that should be used as the <tt class="docutils literal"><span class="pre">wsgi.errors</span></tt> stream. The default
implementation just returns <tt class="docutils literal"><span class="pre">sys.stderr</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.simple_server.WSGIRequestHandler.handle">
<tt class="descname">handle</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.simple_server.WSGIRequestHandler.handle" title="Permalink to this definition">¶</a></dt>
<dd><p>Process the HTTP request.  The default implementation creates a handler instance
using a <a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.handlers</span></tt></a> class to implement the actual WSGI application
interface.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-wsgiref.validate">
<span id="wsgiref-validate-wsgi-conformance-checker"></span><h2>20.4.4. <a class="reference internal" href="#module-wsgiref.validate" title="wsgiref.validate: WSGI conformance checker."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.validate</span></tt></a> &#8212; WSGI conformance checker<a class="headerlink" href="#module-wsgiref.validate" title="Permalink to this headline">¶</a></h2>
<p>When creating new WSGI application objects, frameworks, servers, or middleware,
it can be useful to validate the new code&#8217;s conformance using
<a class="reference internal" href="#module-wsgiref.validate" title="wsgiref.validate: WSGI conformance checker."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.validate</span></tt></a>.  This module provides a function that creates WSGI
application objects that validate communications between a WSGI server or
gateway and a WSGI application object, to check both sides for protocol
conformance.</p>
<p>Note that this utility does not guarantee complete <span class="target" id="index-9"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a> compliance; an
absence of errors from this module does not necessarily mean that errors do not
exist.  However, if this module does produce an error, then it is virtually
certain that either the server or application is not 100% compliant.</p>
<p>This module is based on the <tt class="xref py py-mod docutils literal"><span class="pre">paste.lint</span></tt> module from Ian Bicking&#8217;s &#8220;Python
Paste&#8221; library.</p>
<dl class="function">
<dt id="wsgiref.validate.validator">
<tt class="descclassname">wsgiref.validate.</tt><tt class="descname">validator</tt><big>(</big><em>application</em><big>)</big><a class="headerlink" href="#wsgiref.validate.validator" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap <em>application</em> and return a new WSGI application object.  The returned
application will forward all requests to the original <em>application</em>, and will
check that both the <em>application</em> and the server invoking it are conforming to
the WSGI specification and to RFC 2616.</p>
<p>Any detected nonconformance results in an <a class="reference internal" href="exceptions.html#exceptions.AssertionError" title="exceptions.AssertionError"><tt class="xref py py-exc docutils literal"><span class="pre">AssertionError</span></tt></a> being raised;
note, however, that how these errors are handled is server-dependent.  For
example, <a class="reference internal" href="#module-wsgiref.simple_server" title="wsgiref.simple_server: A simple WSGI HTTP server."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.simple_server</span></tt></a> and other servers based on
<a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.handlers</span></tt></a> (that don&#8217;t override the error handling methods to do
something else) will simply output a message that an error has occurred, and
dump the traceback to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> or some other error stream.</p>
<p>This wrapper may also generate output using the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module to
indicate behaviors that are questionable but which may not actually be
prohibited by <span class="target" id="index-10"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.  Unless they are suppressed using Python command-line
options or the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> API, any such warnings will be written to
<tt class="docutils literal"><span class="pre">sys.stderr</span></tt> (<em>not</em> <tt class="docutils literal"><span class="pre">wsgi.errors</span></tt>, unless they happen to be the same
object).</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">wsgiref.validate</span> <span class="kn">import</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>

<span class="c"># Our callable object which is intentionally not compliant to the</span>
<span class="c"># standard, so the validator is going to break</span>
<span class="k">def</span> <span class="nf">simple_app</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">status</span> <span class="o">=</span> <span class="s">&#39;200 OK&#39;</span> <span class="c"># HTTP Status</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s">&#39;text/plain&#39;</span><span class="p">)]</span> <span class="c"># HTTP Headers</span>
    <span class="n">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>

    <span class="c"># This is going to break because we need to return a list, and</span>
    <span class="c"># the validator is going to inform us</span>
    <span class="k">return</span> <span class="s">&quot;Hello World&quot;</span>

<span class="c"># This is the application wrapped in a validator</span>
<span class="n">validator_app</span> <span class="o">=</span> <span class="n">validator</span><span class="p">(</span><span class="n">simple_app</span><span class="p">)</span>

<span class="n">httpd</span> <span class="o">=</span> <span class="n">make_server</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">validator_app</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Listening on port 8000....&quot;</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="module-wsgiref.handlers">
<span id="wsgiref-handlers-server-gateway-base-classes"></span><h2>20.4.5. <a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.handlers</span></tt></a> &#8211; server/gateway base classes<a class="headerlink" href="#module-wsgiref.handlers" title="Permalink to this headline">¶</a></h2>
<p>This module provides base handler classes for implementing WSGI servers and
gateways.  These base classes handle most of the work of communicating with a
WSGI application, as long as they are given a CGI-like environment, along with
input, output, and error streams.</p>
<dl class="class">
<dt id="wsgiref.handlers.CGIHandler">
<em class="property">class </em><tt class="descclassname">wsgiref.handlers.</tt><tt class="descname">CGIHandler</tt><a class="headerlink" href="#wsgiref.handlers.CGIHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>CGI-based invocation via <tt class="docutils literal"><span class="pre">sys.stdin</span></tt>, <tt class="docutils literal"><span class="pre">sys.stdout</span></tt>, <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> and
<tt class="docutils literal"><span class="pre">os.environ</span></tt>.  This is useful when you have a WSGI application and want to run
it as a CGI script.  Simply invoke <tt class="docutils literal"><span class="pre">CGIHandler().run(app)</span></tt>, where <tt class="docutils literal"><span class="pre">app</span></tt> is
the WSGI application object you wish to invoke.</p>
<p>This class is a subclass of <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseCGIHandler</span></tt></a> that sets <tt class="docutils literal"><span class="pre">wsgi.run_once</span></tt>
to true, <tt class="docutils literal"><span class="pre">wsgi.multithread</span></tt> to false, and <tt class="docutils literal"><span class="pre">wsgi.multiprocess</span></tt> to true, and
always uses <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> and <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a> to obtain the necessary CGI streams and
environment.</p>
</dd></dl>

<dl class="class">
<dt id="wsgiref.handlers.BaseCGIHandler">
<em class="property">class </em><tt class="descclassname">wsgiref.handlers.</tt><tt class="descname">BaseCGIHandler</tt><big>(</big><em>stdin</em>, <em>stdout</em>, <em>stderr</em>, <em>environ</em>, <em>multithread=True</em>, <em>multiprocess=False</em><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseCGIHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">CGIHandler</span></tt></a>, but instead of using the <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> and
<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a> modules, the CGI environment and I/O streams are specified explicitly.
The <em>multithread</em> and <em>multiprocess</em> values are used to set the
<tt class="docutils literal"><span class="pre">wsgi.multithread</span></tt> and <tt class="docutils literal"><span class="pre">wsgi.multiprocess</span></tt> flags for any applications run by
the handler instance.</p>
<p>This class is a subclass of <a class="reference internal" href="#wsgiref.handlers.SimpleHandler" title="wsgiref.handlers.SimpleHandler"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHandler</span></tt></a> intended for use with
software other than HTTP &#8220;origin servers&#8221;.  If you are writing a gateway
protocol implementation (such as CGI, FastCGI, SCGI, etc.) that uses a
<tt class="docutils literal"><span class="pre">Status:</span></tt> header to send an HTTP status, you probably want to subclass this
instead of <a class="reference internal" href="#wsgiref.handlers.SimpleHandler" title="wsgiref.handlers.SimpleHandler"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHandler</span></tt></a>.</p>
</dd></dl>

<dl class="class">
<dt id="wsgiref.handlers.SimpleHandler">
<em class="property">class </em><tt class="descclassname">wsgiref.handlers.</tt><tt class="descname">SimpleHandler</tt><big>(</big><em>stdin</em>, <em>stdout</em>, <em>stderr</em>, <em>environ</em>, <em>multithread=True</em>, <em>multiprocess=False</em><big>)</big><a class="headerlink" href="#wsgiref.handlers.SimpleHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseCGIHandler</span></tt></a>, but designed for use with HTTP origin
servers.  If you are writing an HTTP server implementation, you will probably
want to subclass this instead of <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseCGIHandler</span></tt></a></p>
<p>This class is a subclass of <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a>.  It overrides the
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a>, <tt class="xref py py-meth docutils literal"><span class="pre">get_stdin()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">get_stderr()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">add_cgi_vars()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">_write()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">_flush()</span></tt> methods to support explicitly setting the
environment and streams via the constructor.  The supplied environment and
streams are stored in the <tt class="xref py py-attr docutils literal"><span class="pre">stdin</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">stdout</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">stderr</span></tt>, and
<tt class="xref py py-attr docutils literal"><span class="pre">environ</span></tt> attributes.</p>
</dd></dl>

<dl class="class">
<dt id="wsgiref.handlers.BaseHandler">
<em class="property">class </em><tt class="descclassname">wsgiref.handlers.</tt><tt class="descname">BaseHandler</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>This is an abstract base class for running WSGI applications.  Each instance
will handle a single HTTP request, although in principle you could create a
subclass that was reusable for multiple requests.</p>
<p><a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a> instances have only one method intended for external use:</p>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.run">
<tt class="descname">run</tt><big>(</big><em>app</em><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.run" title="Permalink to this definition">¶</a></dt>
<dd><p>Run the specified WSGI application, <em>app</em>.</p>
</dd></dl>

<p>All of the other <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a> methods are invoked by this method in the
process of running the application, and thus exist primarily to allow
customizing the process.</p>
<p>The following methods MUST be overridden in a subclass:</p>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler._write">
<tt class="descname">_write</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler._write" title="Permalink to this definition">¶</a></dt>
<dd><p>Buffer the string <em>data</em> for transmission to the client.  It&#8217;s okay if this
method actually transmits the data; <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a> just separates write
and flush operations for greater efficiency when the underlying system actually
has such a distinction.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler._flush">
<tt class="descname">_flush</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler._flush" title="Permalink to this definition">¶</a></dt>
<dd><p>Force buffered data to be transmitted to the client.  It&#8217;s okay if this method
is a no-op (i.e., if <a class="reference internal" href="#wsgiref.handlers.BaseHandler._write" title="wsgiref.handlers.BaseHandler._write"><tt class="xref py py-meth docutils literal"><span class="pre">_write()</span></tt></a> actually sends the data).</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.get_stdin">
<tt class="descname">get_stdin</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.get_stdin" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an input stream object suitable for use as the <tt class="docutils literal"><span class="pre">wsgi.input</span></tt> of the
request currently being processed.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.get_stderr">
<tt class="descname">get_stderr</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.get_stderr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an output stream object suitable for use as the <tt class="docutils literal"><span class="pre">wsgi.errors</span></tt> of the
request currently being processed.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.add_cgi_vars">
<tt class="descname">add_cgi_vars</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.add_cgi_vars" title="Permalink to this definition">¶</a></dt>
<dd><p>Insert CGI variables for the current request into the <tt class="xref py py-attr docutils literal"><span class="pre">environ</span></tt> attribute.</p>
</dd></dl>

<p>Here are some other methods and attributes you may wish to override. This list
is only a summary, however, and does not include every method that can be
overridden.  You should consult the docstrings and source code for additional
information before attempting to create a customized <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a>
subclass.</p>
<p>Attributes and methods for customizing the WSGI environment:</p>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_multithread">
<tt class="descname">wsgi_multithread</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_multithread" title="Permalink to this definition">¶</a></dt>
<dd><p>The value to be used for the <tt class="docutils literal"><span class="pre">wsgi.multithread</span></tt> environment variable.  It
defaults to true in <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a>, but may have a different default (or
be set by the constructor) in the other subclasses.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_multiprocess">
<tt class="descname">wsgi_multiprocess</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_multiprocess" title="Permalink to this definition">¶</a></dt>
<dd><p>The value to be used for the <tt class="docutils literal"><span class="pre">wsgi.multiprocess</span></tt> environment variable.  It
defaults to true in <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a>, but may have a different default (or
be set by the constructor) in the other subclasses.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_run_once">
<tt class="descname">wsgi_run_once</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_run_once" title="Permalink to this definition">¶</a></dt>
<dd><p>The value to be used for the <tt class="docutils literal"><span class="pre">wsgi.run_once</span></tt> environment variable.  It
defaults to false in <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a>, but <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">CGIHandler</span></tt></a> sets it to
true by default.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.os_environ">
<tt class="descname">os_environ</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.os_environ" title="Permalink to this definition">¶</a></dt>
<dd><p>The default environment variables to be included in every request&#8217;s WSGI
environment.  By default, this is a copy of <tt class="docutils literal"><span class="pre">os.environ</span></tt> at the time that
<a class="reference internal" href="#module-wsgiref.handlers" title="wsgiref.handlers: WSGI server/gateway base classes."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.handlers</span></tt></a> was imported, but subclasses can either create their own
at the class or instance level.  Note that the dictionary should be considered
read-only, since the default value is shared between multiple classes and
instances.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.server_software">
<tt class="descname">server_software</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.server_software" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <a class="reference internal" href="#wsgiref.handlers.BaseHandler.origin_server" title="wsgiref.handlers.BaseHandler.origin_server"><tt class="xref py py-attr docutils literal"><span class="pre">origin_server</span></tt></a> attribute is set, this attribute&#8217;s value is used to
set the default <tt class="docutils literal"><span class="pre">SERVER_SOFTWARE</span></tt> WSGI environment variable, and also to set a
default <tt class="docutils literal"><span class="pre">Server:</span></tt> header in HTTP responses.  It is ignored for handlers (such
as <a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseCGIHandler</span></tt></a> and <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">CGIHandler</span></tt></a>) that are not HTTP origin
servers.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.get_scheme">
<tt class="descname">get_scheme</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.get_scheme" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the URL scheme being used for the current request.  The default
implementation uses the <tt class="xref py py-func docutils literal"><span class="pre">guess_scheme()</span></tt> function from <a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.util</span></tt></a>
to guess whether the scheme should be &#8220;http&#8221; or &#8220;https&#8221;, based on the current
request&#8217;s <tt class="xref py py-attr docutils literal"><span class="pre">environ</span></tt> variables.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.setup_environ">
<tt class="descname">setup_environ</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.setup_environ" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <tt class="xref py py-attr docutils literal"><span class="pre">environ</span></tt> attribute to a fully-populated WSGI environment.  The
default implementation uses all of the above methods and attributes, plus the
<a class="reference internal" href="#wsgiref.handlers.BaseHandler.get_stdin" title="wsgiref.handlers.BaseHandler.get_stdin"><tt class="xref py py-meth docutils literal"><span class="pre">get_stdin()</span></tt></a>, <a class="reference internal" href="#wsgiref.handlers.BaseHandler.get_stderr" title="wsgiref.handlers.BaseHandler.get_stderr"><tt class="xref py py-meth docutils literal"><span class="pre">get_stderr()</span></tt></a>, and <a class="reference internal" href="#wsgiref.handlers.BaseHandler.add_cgi_vars" title="wsgiref.handlers.BaseHandler.add_cgi_vars"><tt class="xref py py-meth docutils literal"><span class="pre">add_cgi_vars()</span></tt></a> methods and the
<a class="reference internal" href="#wsgiref.handlers.BaseHandler.wsgi_file_wrapper" title="wsgiref.handlers.BaseHandler.wsgi_file_wrapper"><tt class="xref py py-attr docutils literal"><span class="pre">wsgi_file_wrapper</span></tt></a> attribute.  It also inserts a <tt class="docutils literal"><span class="pre">SERVER_SOFTWARE</span></tt> key
if not present, as long as the <a class="reference internal" href="#wsgiref.handlers.BaseHandler.origin_server" title="wsgiref.handlers.BaseHandler.origin_server"><tt class="xref py py-attr docutils literal"><span class="pre">origin_server</span></tt></a> attribute is a true value
and the <a class="reference internal" href="#wsgiref.handlers.BaseHandler.server_software" title="wsgiref.handlers.BaseHandler.server_software"><tt class="xref py py-attr docutils literal"><span class="pre">server_software</span></tt></a> attribute is set.</p>
</dd></dl>

<p>Methods and attributes for customizing exception handling:</p>
<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.log_exception">
<tt class="descname">log_exception</tt><big>(</big><em>exc_info</em><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.log_exception" title="Permalink to this definition">¶</a></dt>
<dd><p>Log the <em>exc_info</em> tuple in the server log.  <em>exc_info</em> is a <tt class="docutils literal"><span class="pre">(type,</span> <span class="pre">value,</span>
<span class="pre">traceback)</span></tt> tuple.  The default implementation simply writes the traceback to
the request&#8217;s <tt class="docutils literal"><span class="pre">wsgi.errors</span></tt> stream and flushes it.  Subclasses can override
this method to change the format or retarget the output, mail the traceback to
an administrator, or whatever other action may be deemed suitable.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.traceback_limit">
<tt class="descname">traceback_limit</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.traceback_limit" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum number of frames to include in tracebacks output by the default
<a class="reference internal" href="#wsgiref.handlers.BaseHandler.log_exception" title="wsgiref.handlers.BaseHandler.log_exception"><tt class="xref py py-meth docutils literal"><span class="pre">log_exception()</span></tt></a> method.  If <tt class="xref docutils literal"><span class="pre">None</span></tt>, all frames are included.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.error_output">
<tt class="descname">error_output</tt><big>(</big><em>environ</em>, <em>start_response</em><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_output" title="Permalink to this definition">¶</a></dt>
<dd><p>This method is a WSGI application to generate an error page for the user.  It is
only invoked if an error occurs before headers are sent to the client.</p>
<p>This method can access the current error information using <tt class="docutils literal"><span class="pre">sys.exc_info()</span></tt>,
and should pass that information to <em>start_response</em> when calling it (as
described in the &#8220;Error Handling&#8221; section of <span class="target" id="index-11"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>).</p>
<p>The default implementation just uses the <a class="reference internal" href="#wsgiref.handlers.BaseHandler.error_status" title="wsgiref.handlers.BaseHandler.error_status"><tt class="xref py py-attr docutils literal"><span class="pre">error_status</span></tt></a>,
<a class="reference internal" href="#wsgiref.handlers.BaseHandler.error_headers" title="wsgiref.handlers.BaseHandler.error_headers"><tt class="xref py py-attr docutils literal"><span class="pre">error_headers</span></tt></a>, and <a class="reference internal" href="#wsgiref.handlers.BaseHandler.error_body" title="wsgiref.handlers.BaseHandler.error_body"><tt class="xref py py-attr docutils literal"><span class="pre">error_body</span></tt></a> attributes to generate an output
page.  Subclasses can override this to produce more dynamic error output.</p>
<p>Note, however, that it&#8217;s not recommended from a security perspective to spit out
diagnostics to any old user; ideally, you should have to do something special to
enable diagnostic output, which is why the default implementation doesn&#8217;t
include any.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.error_status">
<tt class="descname">error_status</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_status" title="Permalink to this definition">¶</a></dt>
<dd><p>The HTTP status used for error responses.  This should be a status string as
defined in <span class="target" id="index-12"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>; it defaults to a 500 code and message.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.error_headers">
<tt class="descname">error_headers</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>The HTTP headers used for error responses.  This should be a list of WSGI
response headers (<tt class="docutils literal"><span class="pre">(name,</span> <span class="pre">value)</span></tt> tuples), as described in <span class="target" id="index-13"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>.  The
default list just sets the content type to <tt class="docutils literal"><span class="pre">text/plain</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.error_body">
<tt class="descname">error_body</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.error_body" title="Permalink to this definition">¶</a></dt>
<dd><p>The error response body.  This should be an HTTP response body string. It
defaults to the plain text, &#8220;A server error occurred.  Please contact the
administrator.&#8221;</p>
</dd></dl>

<p>Methods and attributes for <span class="target" id="index-14"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a>&#8216;s &#8220;Optional Platform-Specific File
Handling&#8221; feature:</p>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.wsgi_file_wrapper">
<tt class="descname">wsgi_file_wrapper</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.wsgi_file_wrapper" title="Permalink to this definition">¶</a></dt>
<dd><p>A <tt class="docutils literal"><span class="pre">wsgi.file_wrapper</span></tt> factory, or <tt class="xref docutils literal"><span class="pre">None</span></tt>.  The default value of this
attribute is the <tt class="xref py py-class docutils literal"><span class="pre">FileWrapper</span></tt> class from <a class="reference internal" href="#module-wsgiref.util" title="wsgiref.util: WSGI environment utilities."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref.util</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="wsgiref.handlers.BaseHandler.sendfile">
<tt class="descname">sendfile</tt><big>(</big><big>)</big><a class="headerlink" href="#wsgiref.handlers.BaseHandler.sendfile" title="Permalink to this definition">¶</a></dt>
<dd><p>Override to implement platform-specific file transmission.  This method is
called only if the application&#8217;s return value is an instance of the class
specified by the <a class="reference internal" href="#wsgiref.handlers.BaseHandler.wsgi_file_wrapper" title="wsgiref.handlers.BaseHandler.wsgi_file_wrapper"><tt class="xref py py-attr docutils literal"><span class="pre">wsgi_file_wrapper</span></tt></a> attribute.  It should return a true
value if it was able to successfully transmit the file, so that the default
transmission code will not be executed. The default implementation of this
method just returns a false value.</p>
</dd></dl>

<p>Miscellaneous methods and attributes:</p>
<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.origin_server">
<tt class="descname">origin_server</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.origin_server" title="Permalink to this definition">¶</a></dt>
<dd><p>This attribute should be set to a true value if the handler&#8217;s <a class="reference internal" href="#wsgiref.handlers.BaseHandler._write" title="wsgiref.handlers.BaseHandler._write"><tt class="xref py py-meth docutils literal"><span class="pre">_write()</span></tt></a> and
<a class="reference internal" href="#wsgiref.handlers.BaseHandler._flush" title="wsgiref.handlers.BaseHandler._flush"><tt class="xref py py-meth docutils literal"><span class="pre">_flush()</span></tt></a> are being used to communicate directly to the client, rather than
via a CGI-like gateway protocol that wants the HTTP status in a special
<tt class="docutils literal"><span class="pre">Status:</span></tt> header.</p>
<p>This attribute&#8217;s default value is true in <a class="reference internal" href="#wsgiref.handlers.BaseHandler" title="wsgiref.handlers.BaseHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHandler</span></tt></a>, but false in
<a class="reference internal" href="#wsgiref.handlers.BaseCGIHandler" title="wsgiref.handlers.BaseCGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseCGIHandler</span></tt></a> and <a class="reference internal" href="#wsgiref.handlers.CGIHandler" title="wsgiref.handlers.CGIHandler"><tt class="xref py py-class docutils literal"><span class="pre">CGIHandler</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="wsgiref.handlers.BaseHandler.http_version">
<tt class="descname">http_version</tt><a class="headerlink" href="#wsgiref.handlers.BaseHandler.http_version" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#wsgiref.handlers.BaseHandler.origin_server" title="wsgiref.handlers.BaseHandler.origin_server"><tt class="xref py py-attr docutils literal"><span class="pre">origin_server</span></tt></a> is true, this string attribute is used to set the HTTP
version of the response set to the client.  It defaults to <tt class="docutils literal"><span class="pre">&quot;1.0&quot;</span></tt>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="examples">
<h2>20.4.6. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>This is a working &#8220;Hello World&#8221; WSGI application:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>

<span class="c"># Every WSGI application must have an application object - a callable</span>
<span class="c"># object that accepts two arguments. For that purpose, we&#39;re going to</span>
<span class="c"># use a function (note that you&#39;re not limited to a function, you can</span>
<span class="c"># use a class for example). The first argument passed to the function</span>
<span class="c"># is a dictionary containing CGI-style envrironment variables and the</span>
<span class="c"># second variable is the callable object (see PEP 333).</span>
<span class="k">def</span> <span class="nf">hello_world_app</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">status</span> <span class="o">=</span> <span class="s">&#39;200 OK&#39;</span> <span class="c"># HTTP Status</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s">&#39;text/plain&#39;</span><span class="p">)]</span> <span class="c"># HTTP Headers</span>
    <span class="n">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>

    <span class="c"># The returned object is going to be printed</span>
    <span class="k">return</span> <span class="p">[</span><span class="s">&quot;Hello World&quot;</span><span class="p">]</span>

<span class="n">httpd</span> <span class="o">=</span> <span class="n">make_server</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="n">hello_world_app</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Serving on port 8000...&quot;</span>

<span class="c"># Serve until process is killed</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">20.4. <tt class="docutils literal"><span class="pre">wsgiref</span></tt> &#8212; WSGI Utilities and Reference Implementation</a><ul>
<li><a class="reference internal" href="#module-wsgiref.util">20.4.1. <tt class="docutils literal"><span class="pre">wsgiref.util</span></tt> &#8211; WSGI environment utilities</a></li>
<li><a class="reference internal" href="#module-wsgiref.headers">20.4.2. <tt class="docutils literal"><span class="pre">wsgiref.headers</span></tt> &#8211; WSGI response header tools</a></li>
<li><a class="reference internal" href="#module-wsgiref.simple_server">20.4.3. <tt class="docutils literal"><span class="pre">wsgiref.simple_server</span></tt> &#8211; a simple WSGI HTTP server</a></li>
<li><a class="reference internal" href="#module-wsgiref.validate">20.4.4. <tt class="docutils literal"><span class="pre">wsgiref.validate</span></tt> &#8212; WSGI conformance checker</a></li>
<li><a class="reference internal" href="#module-wsgiref.handlers">20.4.5. <tt class="docutils literal"><span class="pre">wsgiref.handlers</span></tt> &#8211; server/gateway base classes</a></li>
<li><a class="reference internal" href="#examples">20.4.6. Examples</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="cgitb.html"
                        title="previous chapter">20.3. <tt class="docutils literal docutils literal docutils literal"><span class="pre">cgitb</span></tt> &#8212; Traceback manager for CGI scripts</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="urllib.html"
                        title="next chapter">20.5. <tt class="docutils literal docutils literal docutils literal"><span class="pre">urllib</span></tt> &#8212; Open arbitrary resources by URL</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/wsgiref.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="urllib.html" title="20.5. urllib — Open arbitrary resources by URL"
             >next</a> |</li>
        <li class="right" >
          <a href="cgitb.html" title="20.3. cgitb — Traceback manager for CGI scripts"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="internet.html" >20. Internet Protocols and Support</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2014, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 29, 2014.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>